package utils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * 使用lambda表达式对列表进行迭代
 */
public class App3 {

    public static void main(String[] args) {
        // Java 8之前：
        List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
        for (String feature : features) {
            System.out.println(feature);
        }

        //java 8 1
        features.forEach(n -> System.out.println(n));

        features.forEach(n -> {
            if (n.equals("Lambdas")) {
                System.out.println("过滤的值:" + n);
            }
        });

        features.forEach(System.out::print);

        //map
        Map<String, Integer> items = new HashMap<>();
        items.put("A", 10);
        items.put("B", 20);
        items.put("C", 30);
        items.put("D", 40);
        items.put("E", 50);
        items.put("F", 60);

        for (Map.Entry<String, Integer> entry : items.entrySet()) {
            System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
        }

        items.forEach((k, v) -> System.out.println("map的key=" + k + " map的value=" + v));
        items.forEach((k, v) -> {
            System.out.println("map的key=" + k + " map的value=" + v);
            if (k.equals("F")) {
                System.out.println("过滤的map value值:" + v);
            }
        });

        //list
        List<String> items1 = new ArrayList<>();

        items1.add("A");
        items1.add("B");
        items1.add("C");
        items1.add("D");
        items1.add("E");

        //lambda
        //Output : A,B,C,D,E
        items1.forEach(item1 -> System.out.println(item1));

        //Output : C
        items1.forEach(item1 -> {
            if ("C".equals(item1)) {
                System.out.println(item1);
            }
        });

        //method reference
        //Output : A,B,C,D,E
        items1.forEach(System.out::println);

        //Stream and filter
        //Output : B
        items1.stream()
                .filter(s -> s.contains("B"))
                .forEach(System.out::println);
    }
}
